package com.h3c.gm.service

import com.google.common.collect.Lists
import com.google.common.collect.Maps
import com.h3c.gm.dao.IBusOrgDao
import com.h3c.gm.dao.IBusStorageSpecDao
import com.h3c.gm.entity.DO.BusStorageSpecDO
import org.springframework.stereotype.Service

import javax.annotation.Resource
import java.text.DecimalFormat

/**
 * Created by d14287 on 2017/6/26.
 */
@Service
class BusStorageSpecService {

    @Resource
    IBusStorageSpecDao busStorageSpecDao
    @Resource
    IBusOrgDao busOrgDao

    Map<String, Object> getStarageStatistics() {
        def map = Maps.newHashMap()
        long capacity = 0L
        long usedCapacity = 0L
        map.put('count', busStorageSpecDao.getCount(Maps.newHashMap()))
        map.put('capacity', capacity = (busStorageSpecDao.getCapacity(Maps.newHashMap()) == null ? 0L : busStorageSpecDao.getCapacity(Maps.newHashMap()).longValue()))
        map.put('usedCapacity', usedCapacity = (busStorageSpecDao.getUsedCapacity(Maps.newHashMap()) == null ? 0L : busStorageSpecDao.getUsedCapacity(Maps.newHashMap()).longValue()))
        map.put('unUsedCapacity', capacity - usedCapacity)
        map.put('ratio', capacity == 0L ? 0 : new DecimalFormat('#.00').format(usedCapacity / capacity))

        return map

    }

    Map<String, Object> getProStarageStatistics(String province) {
        def orgkeyList = getOrgkeyList(province)
        def queryParams = Maps.newHashMap()
        queryParams.put('orgkeyList', orgkeyList)

        def map = Maps.newHashMap()
        long capacity = 0L
        long usedCapacity = 0L
        map.put('count', busStorageSpecDao.getCount(queryParams))
        map.put('capacity', capacity = (busStorageSpecDao.getCapacity(queryParams) == null ? 0L : busStorageSpecDao.getCapacity(queryParams).longValue()))
        map.put('usedCapacity', usedCapacity = (busStorageSpecDao.getUsedCapacity(queryParams) == null ? 0L : busStorageSpecDao.getUsedCapacity(queryParams).longValue()))
        map.put('unUsedCapacity', capacity - usedCapacity)
        map.put('ratio', capacity == 0L ? 0 : new DecimalFormat('#.00').format(usedCapacity / capacity))

        return map
    }

    List<Map<String, Object>> getMonthstatistics(Date from, Date to) {
        def queryParams = Maps.newHashMap()
        queryParams.put('from', from)
        queryParams.put('to', to)
        busStorageSpecDao.getMonthstatistics(queryParams)
    }

    List<Map<String, Object>> getProMonthstatistics(Date from, Date to, String province) {
        def orgkeyList = getOrgkeyList(province)
        def queryParams = Maps.newHashMap()
        queryParams.put('from', from)
        queryParams.put('to', to)
        queryParams.put('orgkeyList', orgkeyList)
        busStorageSpecDao.getMonthstatistics(queryParams)

    }

    void insert(BusStorageSpecDO busStorageSpecDO) {
        busStorageSpecDao.insert(busStorageSpecDO)
    }

    List<String> getOrgkeyList(String province) {
        def busOrgDOs = busOrgDao.getCityOrg(province)
        def orgkeys = Lists.newArrayList()
        busOrgDOs.each {
            item ->
                orgkeys.add(item.getOrgkey())
        }
        return orgkeys
    }

    void deleteByOrgkey(String orgkey) {
        busStorageSpecDao.deleteByOrgkey(orgkey)
    }
}
